﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDToaThuocCT
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dedongia;
        private long _deid;
        private int _deiddp;
        private long _deidtoathuoc;
        private decimal _desldathanhtoan;
        private decimal _desoluong;
        private decimal _desoluongchieu;
        private decimal _desoluongsang;
        private decimal _desoluongtoi;
        private decimal _desoluongtrua;
        private int _idadongtien;
        private int _iiddangnhap;
        private int _isongay;
        private string _scachdung;
        private string _sidttvp;
        private string _sngaycn;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDToaThuocCT()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDToaThuocCT(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDToaThuocCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1 and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDToaThuocCT.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDToaThuocCT.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDToaThuocCT.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDToaThuocCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDTOATHUOC, ",", columnIDDP, ",", columnSOLUONGSANG, ",", columnSOLUONGTRUA, ",", columnSOLUONGCHIEU, 
                    ",", columnSOLUONGTOI, ",", columnSOLUONG, ",", columnSONGAY, ",", columnDONGIA, ",", columnCACHDUNG, ",", columnDADONGTIEN, ",", columnIDTTVP, ",", columnSLDATHANHTOAN, 
                    ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ") values(", this._deid, ",", this._deidtoathuoc, ",", this._deiddp, ",:", columnSOLUONGSANG, ",:", columnSOLUONGTRUA, ",:", columnSOLUONGCHIEU, 
                    ",:", columnSOLUONGTOI, ",:", columnSOLUONG, ",:", columnSONGAY, ",:", columnDONGIA, ",:", columnCACHDUNG, ",:", columnDADONGTIEN, ",'", this._sidttvp, "',:", columnSLDATHANHTOAN, 
                    ",", this._iiddangnhap, ",:", columnTENMAYTINH, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnCACHDUNG, NpgsqlDbType.Text, 200).Value = this._scachdung;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONGSANG, NpgsqlDbType.Numeric, 20).Value = this._desoluongsang;
                command.Parameters.Add(columnSOLUONGTRUA, NpgsqlDbType.Numeric, 20).Value = this._desoluongtrua;
                command.Parameters.Add(columnSOLUONGCHIEU, NpgsqlDbType.Numeric, 20).Value = this._desoluongchieu;
                command.Parameters.Add(columnSOLUONGTOI, NpgsqlDbType.Numeric, 20).Value = this._desoluongtoi;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 20).Value = this._desoluong;
                command.Parameters.Add(columnSONGAY, NpgsqlDbType.Numeric, 20).Value = this._isongay;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 20).Value = this._dedongia;
                command.Parameters.Add(columnDADONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._idadongtien;
                command.Parameters.Add(columnSLDATHANHTOAN, NpgsqlDbType.Numeric, 20).Value = this._desldathanhtoan;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDToaThuocCT.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._deidtoathuoc = 0L;
            this._deiddp = 0;
            this._desoluongsang = 0M;
            this._desoluongtrua = 0M;
            this._desoluongchieu = 0M;
            this._desoluongtoi = 0M;
            this._desoluong = 0M;
            this._isongay = 0;
            this._dedongia = 0M;
            this._scachdung = "";
            this._idadongtien = 0;
            this._sidttvp = "";
            this._desldathanhtoan = 0M;
            this._iiddangnhap = 0;
            this._stenmaytinh = "";
            this._sngaycn = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDToaThuocCT.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDTOATHUOC, ",", columnIDDP, ",", columnSOLUONGSANG, ",", columnSOLUONGTRUA, ",", columnSOLUONGCHIEU, ",", columnSOLUONGTOI, ",", columnSOLUONG, 
                    ",", columnSONGAY, ",", columnDONGIA, ",", columnCACHDUNG, ",", columnDADONGTIEN, ",", columnIDTTVP, ",", columnSLDATHANHTOAN, ",", columnIDDANGNHAP, ",", columnTENMAYTINH, 
                    ",to_char(", columnNGAYCN, ",'dd/mm/yyyy hh24:mi') as ", columnNGAYCN, " from ", this._sSchema, ".", tableName, " where ", columnID, "=", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                    this._deidtoathuoc = long.Parse(dataTable.Rows[0][columnIDTOATHUOC].ToString());
                    this._deiddp = int.Parse(dataTable.Rows[0][columnIDDP].ToString());
                    this._desoluongsang = decimal.Parse(dataTable.Rows[0][columnSOLUONGSANG].ToString());
                    this._desoluongtrua = decimal.Parse(dataTable.Rows[0][columnSOLUONGTRUA].ToString());
                    this._desoluongchieu = decimal.Parse(dataTable.Rows[0][columnSOLUONGCHIEU].ToString());
                    this._desoluongtoi = decimal.Parse(dataTable.Rows[0][columnSOLUONGTOI].ToString());
                    this._desoluong = decimal.Parse(dataTable.Rows[0][columnSOLUONG].ToString());
                    this._isongay = int.Parse(dataTable.Rows[0][columnSONGAY].ToString());
                    this._dedongia = decimal.Parse(dataTable.Rows[0][columnDONGIA].ToString());
                    this._scachdung = dataTable.Rows[0][columnCACHDUNG].ToString();
                    this._idadongtien = int.Parse(dataTable.Rows[0][columnDADONGTIEN].ToString());
                    this._sidttvp = dataTable.Rows[0][columnIDTTVP].ToString();
                    this._desldathanhtoan = decimal.Parse(dataTable.Rows[0][columnSLDATHANHTOAN].ToString());
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDToaThuocCT.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDToaThuocCT.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDTOATHUOC + "," + columnIDDP + "," + columnSOLUONGSANG + "," + columnSOLUONGTRUA + "," + columnSOLUONGCHIEU + "," + columnSOLUONGTOI + "," + columnSOLUONG + "," + columnSONGAY + "," + columnDONGIA + "," + columnCACHDUNG + "," + columnDADONGTIEN + "," + columnIDTTVP + "," + columnSLDATHANHTOAN + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCN + " from " + this._sSchema + "." + tableName + " order by " + columnID, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDToaThuocCT.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDToaThuocCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDTOATHUOC, " = ", this._deidtoathuoc, ",", columnIDDP, " = ", this._deiddp, ",", columnSOLUONGSANG, " = :", columnSOLUONGSANG, 
                    ",", columnSOLUONGTRUA, " = :", columnSOLUONGTRUA, ",", columnSOLUONGCHIEU, " = :", columnSOLUONGCHIEU, ",", columnSOLUONGTOI, " = :", columnSOLUONGTOI, ",", columnSOLUONG, " = :", columnSOLUONG, 
                    ",", columnSONGAY, " = :", columnSONGAY, ",", columnDONGIA, " = :", columnDONGIA, ",", columnCACHDUNG, " = :", columnCACHDUNG, ",", columnDADONGTIEN, " = :", columnDADONGTIEN, 
                    ",", columnIDTTVP, " = '", this._sidttvp, "',", columnSLDATHANHTOAN, " = :", columnSLDATHANHTOAN, ",", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, 
                    ",", columnNGAYCN, " = now()  where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnCACHDUNG, NpgsqlDbType.Text, 200).Value = this._scachdung;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONGSANG, NpgsqlDbType.Numeric, 20).Value = this._desoluongsang;
                command.Parameters.Add(columnSOLUONGTRUA, NpgsqlDbType.Numeric, 20).Value = this._desoluongtrua;
                command.Parameters.Add(columnSOLUONGCHIEU, NpgsqlDbType.Numeric, 20).Value = this._desoluongchieu;
                command.Parameters.Add(columnSOLUONGTOI, NpgsqlDbType.Numeric, 20).Value = this._desoluongtoi;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 20).Value = this._desoluong;
                command.Parameters.Add(columnSONGAY, NpgsqlDbType.Numeric, 20).Value = this._isongay;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 20).Value = this._dedongia;
                command.Parameters.Add(columnDADONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._idadongtien;
                command.Parameters.Add(columnSLDATHANHTOAN, NpgsqlDbType.Numeric, 20).Value = this._desldathanhtoan;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDToaThuocCT.cs");
                return false;
            }
        }

        public static string columnCACHDUNG
        {
            get
            {
                return "cachdung";
            }
        }

        public static string columnDADONGTIEN
        {
            get
            {
                return "dadongtien";
            }
        }

        public static string columnDONGIA
        {
            get
            {
                return "dongia";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDDP
        {
            get
            {
                return "iddp";
            }
        }

        public static string columnIDTOATHUOC
        {
            get
            {
                return "idtoathuoc";
            }
        }

        public static string columnIDTTVP
        {
            get
            {
                return "idttvp";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnSLDATHANHTOAN
        {
            get
            {
                return "sldathanhtoan";
            }
        }

        public static string columnSOLUONG
        {
            get
            {
                return "soluong";
            }
        }

        public static string columnSOLUONGCHIEU
        {
            get
            {
                return "soluongchieu";
            }
        }

        public static string columnSOLUONGSANG
        {
            get
            {
                return "soluongsang";
            }
        }

        public static string columnSOLUONGTOI
        {
            get
            {
                return "soluongtoi";
            }
        }

        public static string columnSOLUONGTRUA
        {
            get
            {
                return "soluongtrua";
            }
        }

        public static string columnSONGAY
        {
            get
            {
                return "songay";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        private string NGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public string pCACHDUNG
        {
            get
            {
                return this._scachdung;
            }
            set
            {
                this._scachdung = value;
            }
        }

        private int pDADONGTIEN
        {
            get
            {
                return this._idadongtien;
            }
            set
            {
                this._idadongtien = value;
            }
        }

        public decimal pDONGIA
        {
            get
            {
                return this._dedongia;
            }
            set
            {
                this._dedongia = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDDP
        {
            get
            {
                return this._deiddp;
            }
            set
            {
                this._deiddp = value;
            }
        }

        public long pIDTOATHUOC
        {
            get
            {
                return this._deidtoathuoc;
            }
            set
            {
                this._deidtoathuoc = value;
            }
        }

        public string pIDTTVP
        {
            get
            {
                return this._sidttvp;
            }
            set
            {
                this._sidttvp = value;
            }
        }

        public decimal pSLDATHANHTOAN
        {
            get
            {
                return this._desldathanhtoan;
            }
            set
            {
                this._desldathanhtoan = value;
            }
        }

        public decimal pSOLUONG
        {
            get
            {
                return this._desoluong;
            }
            set
            {
                this._desoluong = value;
            }
        }

        public decimal pSOLUONGCHIEU
        {
            get
            {
                return this._desoluongchieu;
            }
            set
            {
                this._desoluongchieu = value;
            }
        }

        public decimal pSOLUONGSANG
        {
            get
            {
                return this._desoluongsang;
            }
            set
            {
                this._desoluongsang = value;
            }
        }

        public decimal pSOLUONGTOI
        {
            get
            {
                return this._desoluongtoi;
            }
            set
            {
                this._desoluongtoi = value;
            }
        }

        public decimal pSOLUONGTRUA
        {
            get
            {
                return this._desoluongtrua;
            }
            set
            {
                this._desoluongtrua = value;
            }
        }

        public int pSONGAY
        {
            get
            {
                return this._isongay;
            }
            set
            {
                this._isongay = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_toathuocct";
            }
        }
    }
}

